package top.ikunikun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.ikunikun.common.Result;
import top.ikunikun.domain.Employee;
import top.ikunikun.service.EmployeeService;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录功能
     * @param request   请求
     * @param employee  用户信息
     * @return          登录说明
     */
    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);
        // 用户账号密码判断
        if(one == null || !one.getPassword().equals(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()))){
            return Result.error("用户名或密码错误");
        }
        if (one.getStatus() == 0){
            return Result.error("用户已冻结");
        }
        request.getSession().setAttribute("employee",one.getId());
        return Result.success(one);
    }

    /**
     * 登出功能
     * @param request   请求
     * @return          登录状态
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }

    /**
     * 新增员工功能
     * @param request   请求
     * @param employee  新增员工信息
     * @return          添加员工结果
     */
    @PostMapping
    public Result<String> save(HttpServletRequest request,@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        log.info("新增员工:{}",employee);
        employeeService.save(employee);
        return Result.success("新增成功");
    }

    /**
     * 分页查询功能
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Employee>> page(int page,int pageSize,String name){
        // 构造分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        // 构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(StringUtils.hasText(name),Employee::getName,name);
        // 添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        // 执行查询，查询结果会自动封装入Page中
        employeeService.page(pageInfo,queryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 修改用户状态
     * @param employee
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return Result.success("用户信息修改成功");
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if(employee == null){
            return Result.error("非法请求");
        }
        return Result.success(employee);
    }
}
